package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;

import play.data.validation.Constraints.Email;
import play.data.validation.Constraints.Required;
import play.db.ebean.Model;
import util.PerfilUsuario;

import com.avaje.ebean.Page;
import com.avaje.ebean.validation.Length;
import com.avaje.ebean.validation.NotEmpty;


@Entity
public class Usuario extends Model {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	public long id;

	@Column(unique = true)
	public String matricula;


	@Required(message="Por favor, informe seu perfil")
	@Enumerated(EnumType.STRING)
	public PerfilUsuario perfil;

	
	@Required(message="Por favor, informe seu nome")
	public String nome;

	@Required(message="Por favor, informe um usuário válido")
	@Column(unique = true)
	@Length(min = 3, max = 50)
	public String usuario;

	@Required(message="Por favor, informe uma senha válida")
	@Length(min = 3, max = 50)
	public String senha;

	public String senhaMD5;

	@Required(message="Por favor, informe um email válido")
	@Email
	public String email;

	@Lob
	public byte[] foto;

	public boolean status = false;

	@Temporal(TemporalType.DATE)
	public Date dtCadastro;

	// ------------------relacionamentos---------------------//
	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	public List<Notificacao> notificacoes = new ArrayList<Notificacao>();

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "aluno")
	public List<Matricula> matriculas = new ArrayList<Matricula>();

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "professor")
	public List<Turma> tumas = new ArrayList<Turma>();

	
	public static Finder<Long, Usuario> find = new Finder<Long, Usuario>(
			Long.class, Usuario.class);

	public static Page<Usuario> page(int page, int pageSize, String sortBy,
			String order, String filter) {
		return find.where().ilike("nome", "%" + filter + "%")
				.orderBy(sortBy + " " + order)
				.findPagingList(pageSize).getPage(page);
	}
}
